<template>
	<view class="all-main">
		<u-loading-page :loading="loadding" loading-mode="semicircle" loading-text="欢迎进入风动社区"></u-loading-page>
		<view v-if="!loadding" class="main">
			<view class="forms">
				<view class="input-item">
					<view class="lefts">
						姓名
					</view>
					<view class="rights">
						<input v-model="inputData.nickname" placeholder="请输入姓名" type="text" />
					</view>
				</view>
				<u-line></u-line>
				<view class="input-item">
					<view class="lefts">
						用户名
					</view>
					<view class="rights">
						<input v-model="inputData.username" placeholder="请输入用户名" type="text" />
					</view>
				</view>
				<u-line></u-line>
				<view class="input-item">
					<view class="lefts">
						手机号
					</view>
					<view class="rights">
						<input v-model="inputData.mobile" placeholder="请输入手机号" type="number" />
					</view>
				</view>
				<u-line></u-line>
				<view class="input-item">
					<view class="lefts">
						密码
					</view>
					<view class="rights">
						<input v-model="inputData.password" placeholder="请输入手机号" type="password" />
					</view>
				</view>
			</view>
			<view class="btn" @click="btnSave">
				提交
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loadding: true,
				customerinfo: {},
				inputData: {
					nickname: '',
					group_id: 9,
					id: '',
					mobile: '',
					username: '',
					password: ''
				},
				is_true: true
			}
		},
		onLoad(e) {
			this.customerinfo = uni.getStorageSync('customerinfo');
			if (e.id) {
				this.inputData.id = e.id
				uni.setNavigationBarTitle({
					title: '编辑业务员'
				})
				this.getData()
			} else {
				uni.setNavigationBarTitle({
					title: '新增业务员'
				})
				this.loadding = false
			}
		},
		methods: {
			// 获取统计数据
			async getData() {
				this.loadding = false
			},
			// 保存信息
			btnSave() {
				var that = this
				if (this.inputData.nickname == '') {
					uni.showToast({
						title: '请输入姓名',
						icon: "none"
					});
					return false
				}
				if (this.inputData.username == '') {
					uni.showToast({
						title: '请输入用户名',
						icon: "none"
					});
					return false
				}
				if (this.inputData.mobile == '') {
					uni.showToast({
						title: '请输入手机号',
						icon: "none"
					});
					return false
				}
				if (this.inputData.password == '') {
					uni.showToast({
						title: '请输入密码',
						icon: "none"
					});
					return false
				}
				uni.showModal({
					title: '提示',
					content: '确定提交吗？',
					success: function(res) {
						if (res.confirm) {
							that.btnSaveFun()
						}
					}
				});
			},
			// 保存信息 - 方法
			async btnSaveFun() {
				var that = this
				if (this.is_true) {
					this.is_true = false
					uni.showLoading({
						title: '加载中'
					});
					const {
						data: res
					} = await this.$myHttp5({
						url: 'v1/createAdmin',
						data: this.inputData,
						header: {
							'token': this.customerinfo.token
						}
					});
					this.is_true = true
					uni.hideLoading()
					uni.showToast({
						title: res.msg,
						icon: "none"
					});
					if (res.code == 1) {
						setTimeout(function() {
							uni.navigateBack({
								delta: 1, // 返回的页面数，如果为1表示返回上一页
							});
						},1500)
					}
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #f3f4f6;
	}
	
	.all-main {
		.main {
			padding: 20rpx;
			.forms {
				background-color: #ffffff;
				.input-item {
					padding: 30rpx;
					display: flex;
					justify-content: space-between;
					.lefts {
						width: 20%;
					}
					.rights {
						width: 80%;
						input {
							width: 100%;
							text-align: right;
						}
					}
				}
			}
			.btn {
				margin-top: 50rpx;
				background-color: #2979ff;
				color: #ffffff;
				font-weight: bold;
				text-align: center;
				padding: 30rpx;
				border-radius: 16rpx;
			}
		}
	}
</style>
